"use strict";

/**
 * @ngdoc directive
 * @name jgappApp.directive:comment
 * @description
 * # comment
 */
angular.module("jgappApp").directive("comment", [
  "DataService",
  "Util",
  "openApi",
  "FileService",
  "$rootScope",
  function(DataService, Util, openApi, FileService, $rootScope) {
    return {
      scope: {
        data: "=commentObj",
        usersArr: "=followObj"
      },
      template:
        '<div class="commentdirective">' +
        '<div class="comment-content">' +
        '<div class="addpic-btn" ng-click="addpic()"></div>' +
        '<div class="comment-input"><textarea ng-model="comment" maxLength="500" placeholder="请填写评论内容"></textarea>' +
        '<div class="reply-wapper" ng-show="usersArr.length>1">' +
        '<span class="c-blue reply">@</span>' +
        '<select multiple ng-multiple="true" ng-model="users" ng-options="item as item.personName for item in usersArr|commentFiler"></select>' +
        "</div></div>" +
        '<div class="submit btn" ng-click="submit()"> 发送 </div> ' +
        "</div>" +
        '<ul class="file-list cl" ng-hide="fileArr.length<1">' +
        '<li ng-repeat="item in fileArr track by $index" ng-class="{img:isImg(item)}"><img ng-click="goDetail(item)" ng-src="{{item|showimg}}" />' +
        '<span class="comment-file-btn" ng-click="delpic(item,$index)"></span>' +
        "</li>" +
        "</ul>" +
        '<hr ng-show="fileArr.length>0&&users.length>0" />' +
        '<div class="joinpeople-list">' +
        '<div class="joinpeople-item tc " ng-repeat="item in users track by item.userId">' +
        '<div class="icon_del1" ng-click="deleteuser(item)">del</div>' +
        '<div class="item-avtar lh50"><img ng-src={{item.portrait}} ng-if="item.portrait"> {{item.personName|username}}</div>' +
        "</div>" +
        "</div>" +
        "</div>" +
        "</div>",
      restrict: "E",
      link: function postLink(scope, element, attrs) {
        scope.maxnum = 16; //最多八张图
        scope.fileArr = []; //图片显示数组
        scope.$watch("fileData", function(newValue, oldValue) {
          if (newValue) {
            scope.fileArr = newValue.split("|"); //图片数组
          } else {
            scope.fileArr = [];
          }
        });
        var delPic = function(item, index) {
          return function() {
            FileService.delfile(item).then(function(data) {
              Util.Toast(data);
              scope.fileArr.splice(index, 1);
              scope.fileData = scope.fileArr.join("|");
            });
          };
        };
        scope.isFile = Util.isFile;
        scope.isImg = Util.isImg;
        /**
         * 查看图片详情
         * @param  {[type]} filename [文件]
         * @return [type]            [description]
         */
        scope.goDetail = function(filename) {
          // 如果点击的是图片则过滤出所有图片预览
          var index = scope.fileArr.indexOf(filename);
          var param = {};
          param.data = scope.fileArr.join("|");
          param.index = index;
          param.showbtn = true;
          NativeUtil.use("photoPreview", null, param);
        };

        /**
         * 添加附件
         * @return [type] [description]
         */
        scope.addpic = function() {
          if (scope.fileArr.length < scope.maxnum) {
            var num = scope.maxnum - scope.fileArr.length;
            //
            var type = parseInt(attrs.type) || 1;
            var param = {
              num: num,
              type: type
            };
            console.log(type);
            NativeUtil.use(
              "choosePhoto",
              function(data) {
                if (scope.fileData) {
                  scope.fileData = scope.fileData + "|" + data;
                } else {
                  scope.fileData = data;
                }
                scope.$apply();
              },
              param
            );
          } else {
            Util.Toast("只能上传" + scope.maxnum + "个附件");
            scope.maxlength = true;
            return;
          }
        };
        scope.delpic = function(item, index) {
          navigator.notification.confirm(
            "确定删除附件",
            delPic(item, index),
            "提示",
            ["确定", "取消"]
          );
        };

        scope.deleteuser = function(user) {
          scope.users = scope.users.filter(function(item) {
            return item.userId !== user.userId;
          });
        };

        scope.submit = function() {
          console.log(scope.data);
          var url = openApi.addForworklog;
          if (!this.comment) {
            Util.Toast("请输入评论内容");
            return;
          }
          var param = {};
          param.contentId = attrs.pkId;
          param.content = this.comment;
          param.attachment = scope.fileData;
          // @某人
          if (scope.users) {
            param.follows = scope.users.map(function(item) {
              return item.userId;
            });
          }
          DataService.getData(openApi.replyReport, param).then(
            function(data, success) {
              Util.Toast("提交评论成功");
              this.data.unshift(data);
              this.reset();
            }.bind(this)
          );
        };
        // 回复评论
        scope.reset = function() {
          this.comment = "";
          scope.fileData = "";
          scope.users = [];
        };
      }
    };
  }
]);
